﻿using System;
using System.Collections.Generic;
using System.Xml;
using DSLPedEditorTest.FakeObjects;
using DSLPedUtilsTest.TestingObjects;
using Dsl.Ped.Editor.Services;
using Dsl.Ped.Framework.AddIns;
using NUnit.Framework;

namespace DSLPedEditorTest.Services
{
    [TestFixture]
    public class ProjectiveObjectServiceTest
    {
        private IAddInRegistrationService addInRegistrationService;
        private const string peoplesFile = "TestingFiles/Peoples.dsl";
        private const string peoplesCorruptedFile = "TestingFiles/PeoplesCorrupted.dsl";
        private const string peoplesNotRegisteredFile = "TestingFiles/PeoplesNotRegistered.dsl";

        [SetUp]
        public void SetUp()
        {
            var nsPeoples = "http://peoples";

            List<Lazy<IDslPedAddIn, IDslPedAddInMetadata>> addIns = new List<Lazy<IDslPedAddIn, IDslPedAddInMetadata>>();
            addIns.Add(new Lazy<IDslPedAddIn, IDslPedAddInMetadata>(new AddInMetadataFake { Extension = "dsl", Namespace = nsPeoples, ProjectiveType = typeof(peoples) }));
            addInRegistrationService = new AddInRegistrationService(addIns);
        }

        [Test]
        public void OpenFileOKTest()
        {
            var projectiveObjectService = new ProjectiveObjectService(addInRegistrationService);
            projectiveObjectService.LoadFromFile(peoplesFile);

            Assert.NotNull(projectiveObjectService.CurrentProjectiveObject);
            Assert.AreEqual(peoplesFile, projectiveObjectService.CurrentFileName);
            Assert.AreEqual(typeof(peoples), projectiveObjectService.CurrentProjectiveType);
        }

        [Test]
        public void OpenFileCorruptedTest()
        {
            var projectiveObjectService = new ProjectiveObjectService(addInRegistrationService);

            try
            {
                projectiveObjectService.LoadFromFile(peoplesCorruptedFile);
            }
            catch (XmlException)
            {
                // We expect this exception
            }

            Assert.IsNull(projectiveObjectService.CurrentProjectiveObject);
            Assert.AreEqual(peoplesCorruptedFile, projectiveObjectService.CurrentFileName);
            Assert.AreEqual(null, projectiveObjectService.CurrentProjectiveType);
        }

        [Test]
        public void OpenFileNotRegisteredTest()
        {
            var projectiveObjectService = new ProjectiveObjectService(addInRegistrationService);

            try
            {
                projectiveObjectService.LoadFromFile(peoplesNotRegisteredFile);
            }
            catch (Exception exc)
            {
                Assert.AreEqual("File type not registered", exc.Message);
            }

            Assert.IsNull(projectiveObjectService.CurrentProjectiveObject);
            Assert.AreEqual(peoplesNotRegisteredFile, projectiveObjectService.CurrentFileName);
            Assert.AreEqual(null, projectiveObjectService.CurrentProjectiveType);
        }
    }
}
